import Utils from "@/utils/index";
import { useEffect } from "react";

const { getQueryVariable, getSystemInfo } = Utils;
/**
 * @description 根据当前是否有token进行跳转
 * @author wenjiahui
 * @param params 需要向url上传递的参数
 * @param path 前端路由页面/page/index/index
 * @param isEdu 是否为局端 局端的登录地址不同
 * 2023-11-02
 */
export default async function useNavLoginPage(
  params: { [key: string]: string } = {},
  pathname = "/pages/index/index",
  isEdu = false
) {
  const token = getQueryVariable("lg_tk");
  let queryStr = "";
  if (Object.keys(params).length > 0) {
    queryStr = "?" + new URLSearchParams(params);
  }

  useEffect(() => {
    if (!token && getSystemInfo() === "h5") {
      const host = window.location.host;
      const pathName = window.location.pathname;
      const protocol = window.location.protocol;
      const loginPage = isEdu
        ? "http://m.lancooedu.com/edu/#/login"
        : "http://m.lancooedu.com/#/login";
      let url = protocol + "//" + host + pathName + queryStr + "#" + pathname;

      const jumpUrl = `${loginPage}?onlySchoolId=&prev_page=${encodeURIComponent(
        url
      )}`;

      window.location.href = jumpUrl;
    }
  }, [token]);
}
